package com.xucranger.ojapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xucranger.ojapi.enity.DTO.*;
import com.xucranger.ojapi.enity.DTO.common.PageResultDTO;
import com.xucranger.ojapi.enity.DTO.common.ResponseDTO;
import com.xucranger.ojapi.enity.VO.AtcoderVO;
import com.xucranger.ojapi.enity.VO.CodeforcesVO;
import com.xucranger.ojapi.enity.VO.CommonContestVO;
import com.xucranger.ojapi.enity.VO.RankVO;
import com.xucranger.ojapi.enity.domain.Contests;
import com.xucranger.ojapi.mapper.ContestsMapper;
import com.xucranger.ojapi.service.AtcoderService;
import com.xucranger.ojapi.service.CodeforcesService;
import com.xucranger.ojapi.service.CommonService;
import com.xucranger.ojapi.utils.SmartBeanUtil;
import com.xucranger.ojapi.utils.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommonServiceImpl implements CommonService {

    @Autowired
    ContestsMapper contestsMapper;
    @Autowired
    CodeforcesService codeforcesService;
    @Autowired
    AtcoderService atcoderService;




    @Override
    public ResponseDTO<PageResultDTO<Contests>> getcontests(Integer pagenum,Integer pagesize) {
        Page page = new Page(pagenum,pagesize);
        List<Contests> entities = contestsMapper.getcontest(page);
        page.setRecords(entities);
        PageResultDTO<Contests> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public Boolean ifincfcontest(String contestname) {
        System.out.println(contestname);
        List<Contests> entities = contestsMapper.ifincfcontest(contestname);
        System.out.println(entities);
        if(entities.isEmpty())
        {
            System.out.println("buzaizai!!!!!!!!!!");
            return Boolean.FALSE;
        }
        else {
            System.out.println("zai!!!!!!!!!!");
            return Boolean.TRUE;
        }
    }

    @Override
    public ResponseDTO<PageResultDTO<CommonContestVO>> querystudentbycontestname(CommonContestQueryDTO queryDTO) {

        if(ifincfcontest(queryDTO.getContestname())){
            queryDTO.setCfcontestname(queryDTO.getContestname());
            queryDTO.setContestdate(queryDTO.getContestdate());
            CodeforcesQueryDTO cfquertDTO = SmartBeanUtil.copy(queryDTO,CodeforcesQueryDTO.class);
            PageResultDTO<CodeforcesVO> resultDTO = codeforcesService.queryStudentByContestname(cfquertDTO).getData();

            return ResponseDTO.succData(SmartBeanUtil.conver2commoncontest(resultDTO));
        }
        else{
            queryDTO.setAccontestname(queryDTO.getContestname());
            queryDTO.setAccontestdate(queryDTO.getContestdate());
            AtcoderQueryDTO acquertDTO = SmartBeanUtil.copy(queryDTO, AtcoderQueryDTO.class);
            System.out.println(acquertDTO);
            PageResultDTO<AtcoderVO> resultDTO = atcoderService.queryContestbycontestname(acquertDTO).getData();

            return ResponseDTO.succData(SmartBeanUtil.conver2commoncontest(resultDTO));
        }

    }

    @Override
    public ResponseDTO<PageResultDTO<Contests>> getcontestsbyid(Integer pagenum, Integer pagesize, String stuid) {
        Page page = new Page(pagenum,pagesize);
        List<Contests> entities = contestsMapper.getcontestbystuid(page,stuid);
        page.setRecords(entities);
        PageResultDTO<Contests> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    @Override
    public ResponseDTO<PageResultDTO<RankVO>> rank(RankDTO rankDTO) {
        Page page = SmartPageUtil.convert2QueryPage(rankDTO);
        List<RankVO> entities = contestsMapper.rank(page,rankDTO.getType());
        page.setRecords(entities);
        PageResultDTO<RankVO> pageResultDTO = SmartPageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

}
